<template>
  <div class="container">
    <!-- <div class="container-1200"> -->
      <div class="admin-container">
      <div class="flexWithJusBetween mg-B20">
        <div class="title">项目资料列表</div>
      </div>

      <div v-show="queryType == false">
        <el-form ref="warehouseForm" label-width="80px" :inline="true">
          <el-form-item label="关键词">
            <el-input
              prefix-icon="el-icon-search"
              v-model="query.keyword"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="企业名称">
          <el-select v-model="query.companyId" placeholder="请选择企业" clearable>
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in companyList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="填报时间">
            <el-date-picker
              v-model="query.date"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-search"
            type="primary"
            @click="submitQuery"
            >查询
          </el-button>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-refresh-right"
            type="info"
            @click="resetQuery"
            >重置
          </el-button>
        </el-form>
      </div>

      <el-tabs v-model="status" @tab-click="handleClick">
        <el-tab-pane label="待审" name="1">
          <listTable :tableData="tableData" :dict="dict" @audit="audit" type="audit"></listTable>
        </el-tab-pane>
        <el-tab-pane label="已审" name="2">
          <listTable :tableData="tableData" :dict="dict" type="view" ></listTable>
        </el-tab-pane>
        <el-tab-pane label="驳回" name="3">
          <listTable :tableData="tableData" :dict="dict" type="view"></listTable>
        </el-tab-pane>
        <el-tab-pane label="订正" name="4">
          <listTable :tableData="tableData" :dict="dict" @edit="edit" type="edit"></listTable>
        </el-tab-pane>
      </el-tabs>
      <el-footer class="el-footerBox">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page.sync="pageNum"
          @current-change="getProjectList"
        >
        </el-pagination>
      </el-footer>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import utils from "@/utils/utils.js";
import listTable from "./listTable";
import { find as companyFind,list as companyList } from "@/api/adminCompany";
import { list as projectList } from "@/api/adminProject";

export default {
  components: {listTable},
  name: "project",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };
    return {
      queryType: false,
      query: {
        keyword: "",
        date:"",
        startDate:"",
        endDate:"",
        companyId: "",
      },
      companyId: "",
      total:0,
      pageNum:1,
      pageSize:20,
      tableData: [],
      companyInfo: "",
      companyList:[],
      status:'1',

      //字典信息
      dict:{},
      projectStatusDict:[],

      projectStatusDictPromise:"",
    };
  },
  watch: {
    $route: {
      handler: function (route) {},
      immediate: true,
    },
  },

  created() {
    this.projectStatusDictPromise = utils.getDictList("project_status", (res) => {
      this.dict.projectStatusDict = res.data;
    });
  },

  mounted() {
    // if (this.$route.query) {
    //   this.companyId = this.$route.query.companyId;
    //   this.companyFind(this.companyId);
    // }
    let _this = this
    _this.getCmpanyList()
    Promise.all([this.projectStatusDictPromise]).then(
      (res) => {
        //console.log("字典已经全部加载完成--");
        this.getProjectList(this.companyId);
      }
    );
    
  },

  destroyed() {},

  methods: {
    //获取项目列表
    getProjectList(companyId) {
      let _this = this
      let params = new URLSearchParams();
      params.append("status", _this.status);
      params.append("companyId", _this.query.companyId);
      params.append("startDate", _this.query.startDate);
      params.append("endDate", _this.query.endDate);
      params.append("keyword", _this.query.keyword);
      params.append('pageNum', _this.pageNum);
      params.append('pageSize', _this.pageSize);
      projectList(params)
        .then((res) => {
          console.log("ProjectList成功获取数据--", res);
          if (res.type == "success") {
            this.tableData = res.data.data;
            this.total = res.data.total
            if (this.tableData.length > 0) {
              this.tableData.map((item) => {
                item.startDate = utils.dateFormatter(item.startDate);
                item.createDate = utils.dateFormatter(item.createDate);
              });
            }
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },

    //tab切换
    handleClick(tab, even){
      console.log('tab--',this.status);
      this.getProjectList()
    },

    //条件查询
    submitQuery() {
      console.log('this.query.date--',this.query);
      if(!utils.isNull(this.query.date)){
        this.query.startDate = this.query.date[0]
        this.query.endDate = this.query.date[1]
      }
      this.getProjectList();
    },

    //条件查询
    resetQuery() {
      this.query = {
        keyword: "",
        date:"",
        startDate:"",
        endDate:"",
        companyId:""
      },
        this.getProjectList();
    },

    //获取公司列表
    getCmpanyList() {
      companyList().then((res) => {
        console.log("获取公司信息res--", res);
        if (res.type == "success") {
          this.companyList = res.data.data;
        }
      });
    },

    //新增项目
    addProject() {
      this.$router.push({
        path: "/project/add",
        query: { companyId: this.companyId },
      });
    },

    //查看项目
    audit(data) {
      this.$store.dispatch("user/setCloseAliveName", 'projectAudit')
      this.$router.push({
        path: "/project/audit",
        query: { projectId: data.id,companyId: this.companyId, type: "audit" },
      });
    },

    //订正
    edit(data){
      console.log('data--',data);
      this.$store.dispatch("user/setCloseAliveName", 'projectAudit')
      this.$router.push({
        path: "/project/audit",
        query: { projectId: data.id,companyId: this.companyId , type: "edit" },
      });
    },

    viewProjectActivity(data) {
      this.$router.push({
        path: "/projectActivity/auditList",
        query: { projectId: data.id },
      });
    },

    viewProjectExecute(data) {
      this.$router.push({
        path: "/projectExecute/auditList",
        query: { projectId: data.id },
      });
    },

    //编辑项目
    editProject(data) {
      this.$router.push({
        path: "/project/edit",
        query: { projectId: data.id, companyId: this.companyId },
      });
    },

    //项目月报表
    projectExecute(data) {
      this.$router.push({
        path: "/projectExecute/list",
        query: { projectId: data.id, companyId: this.companyId },
      });
    },

    //项目活动表
    projectActivity(data) {
      this.$router.push({
        path: "/projectActivity/list",
        query: { projectId: data.id, companyId: this.companyId },
      });
    },

    //获取公司信息
    companyFind(id) {
      companyFind(id).then((res) => {
        console.log("获取公司信息res--", res);
        if (res.type == "success") {
          this.companyInfo = res.data.company;
        }
      });
    },

    projectStatusFormate(data) {
      return utils.selectDictLabel(this.projectStatusDict, data);
    },
  },
};
</script>

<style lang="scss" scoped>

.el-pagination {
  display:block;
}
</style>
